package com.sgb.kjwl.view.ui.activity.enterprise;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityNeedSignupListBinding;
import com.sgb.kjwl.model.entity.enterprise.NeedDetailSignUpEntity;
import com.swgk.core.util.UserOperating;
import com.sgb.kjwl.view.ui.adapter.enterprise.NeedDetailSignuoAdpater;
import com.sgb.kjwl.viewmodel.enterprise.NeedSignUpListViewModel;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.xrecyclerview.XRecyclerView;

/**
 * 需求详情中报价单位列表
 */
public class NeedSignUpListActivity extends AppActivity {
    private ActivityNeedSignupListBinding mBinding;
    private NeedSignUpListViewModel mViewModel;
    private NeedDetailSignuoAdpater madpater;

    @Override
    protected void initView() {
        super.initView();
        mViewModel = new ViewModelProvider(this).get(NeedSignUpListViewModel.class);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_need_signup_list);
        mBinding.setLifecycleOwner(this);

        mViewModel.itemid = getIntent().getStringExtra("data");
        mViewModel.ishome = getIntent().getBooleanExtra("ishome", false);
        initRecyclerView();
        initobserve();


    }

    @Override
    protected void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    private void initobserve() {

        mViewModel.ToastShow.observe(this, data -> {
            MToast.showLongToast(this, data);
        });

        mViewModel.showloading.observe(this, type_int -> {
            switch (type_int) {
                case 1:
                    DialogHelper.showProgressDialog(NeedSignUpListActivity.this, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
                    break;
                case 2:
                    DialogHelper.dismissProgressDialog();
                    break;
            }
        });

        mViewModel.listdata.observe(this, listdata -> {
            if(listdata.size()<10){
                mBinding.recyclerViewTrand.setNoMore(true);
            }
            if (mViewModel.loadmore) {
                mBinding.recyclerViewTrand.loadMoreComplete();
                madpater.adddata(listdata);

            } else {
                mBinding.recyclerViewTrand.refreshComplete();
                if (listdata.size() == 0) {
                    mBinding.recyclerViewTrand.setVisibility(View.GONE);
                    mBinding.include.setVisibility(View.VISIBLE);
                } else {
                    mBinding.include.setVisibility(View.GONE);
                    mBinding.recyclerViewTrand.setVisibility(View.VISIBLE);
                    madpater.setdata(listdata);
                }
            }
        });


        mViewModel.mData.observe(this, data -> {
            madpater.setCheckuser(ismeno(mViewModel.mData.getValue().getPublishType(), mViewModel.mData.getValue().getCompanyNo(), mViewModel.mData.getValue().getTeamNo(), mViewModel.mData.getValue().getUserNo()));
            madpater.setOfferStatus(data.getOfferStatus() == 2);
            mBinding.actionBar.setTitle(ismeno(mViewModel.mData.getValue().getPublishType(), mViewModel.mData.getValue().getCompanyNo(), mViewModel.mData.getValue().getTeamNo(), mViewModel.mData.getValue().getUserNo())&&!mViewModel.ishome ? "选择报价单位" : "报价单位");
            mViewModel.getListData(false);
        });
    }


    private void initRecyclerView() {
        madpater = new NeedDetailSignuoAdpater(this);
        madpater.setList(true);
        madpater.setIshome(mViewModel.ishome);

        mBinding.recyclerViewTrand.setLayoutManager(new LinearLayoutManager(this));
        mBinding.recyclerViewTrand.setAdapter(madpater);

        mBinding.recyclerViewTrand.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mViewModel.getListData(false);
            }

            @Override
            public void onLoadMore() {
                mViewModel.getListData(true);
            }
        });


        madpater.setOnItemClick(new AdapterOnItemClick<NeedDetailSignUpEntity>() {
            @Override
            public void onItemClick(NeedDetailSignUpEntity needDetailSignUpEntity, int position) {
                if (ismeno(mViewModel.mData.getValue().getPublishType(), mViewModel.mData.getValue().getCompanyNo(), mViewModel.mData.getValue().getTeamNo(), mViewModel.mData.getValue().getUserNo()) && !mViewModel.ishome) {
                    //点击选ta
                    if (position == -1) {
                        PublishPublicityActivity.start(NeedSignUpListActivity.this, needDetailSignUpEntity, mViewModel.mData.getValue());//设置公示
                    } else {
                        SignUpDetailActivity.start(NeedSignUpListActivity.this, needDetailSignUpEntity, mViewModel.mData.getValue());//报价详情
                    }
                }
            }
        });
    }

    /**
     * 判定是否是自己账号
     */
    public boolean ismeno(int publishtype, String companyno, String teamno, String userno) {
        int metype = UserOperating.getInstance().isCompany() ? 1 : UserOperating.getInstance().isTeam() ? 2 : 0;
        switch (publishtype) {
            case 1://公司
                if (metype == 1 && SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.COMPANY_NO, "").equals(companyno)) {
                    return true;
                }
                break;
            case 2://团队
                if (metype == 2 && SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.COMPANY_NO, "").equals(teamno)) {
                    return true;
                }
                break;
            default:
                if (metype == 0 && SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.USER_NO, "").equals(userno)) {
                    return true;
                }
                break;
        }

        return false;
    }

    @Override
    protected void onResume() {
        super.onResume();

        mViewModel.getNeeddetail();
    }

    public static void start(Context mContext, String id) {
        Intent mIntent = new Intent();
        mIntent.setClass(mContext, NeedSignUpListActivity.class);
        mIntent.putExtra("data", id);
        mContext.startActivity(mIntent);

    }

    public static void start(Context mContext, String id, boolean ishome) {
        Intent mIntent = new Intent();
        mIntent.setClass(mContext, NeedSignUpListActivity.class);
        mIntent.putExtra("data", id);
        mIntent.putExtra("ishome", ishome);
        mContext.startActivity(mIntent);

    }
}
